from flask import Flask
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()

def create_app():    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:abc123456@userdb:3306/user'    
    db.init_app(app)
    return app

class Dorm(db.Model):
    __tablename__ = 'dorm'
    id = db.Column(db.Integer, primary_key=True)
    dormid = db.Column(db.String(10))

    def __init__(self, dormid):
        self.dormid = dormid

class Unit(db.Model):
    __tablename__ = 'unit'
    id = db.Column(db.Integer, primary_key=True)
    unitid = db.Column(db.String(10))
    dorm_id = db.Column(db.Integer, db.ForeignKey('dorm.id'))
    dorm = db.relationship('Dorm',
        backref=db.backref('dorm'))

    def __init__(self,unitid, dorm):
        self.unitid = unitid
        self.dorm = dorm

class Room(db.Model):
    __tablename__ = 'room'
    id = db.Column(db.Integer, primary_key=True)
    roomid = db.Column(db.String(10))
    unit_id = db.Column(db.Integer, db.ForeignKey('unit.id'))
    unit = db.relationship('Unit',
        backref=db.backref('unit'))
    gender = db.Column(db.String(1))
    totalbed = db.Column(db.Integer)
    rest = db.Column(db.Integer)
    bad = db.Column(db.Integer)

    def __init__(self, roomid, unit, gender, totalbed = 4, rest = 4, bad = 0):
        self.roomid = roomid
        self.unit = unit
        self.gender = gender
        self.totalbed = totalbed
        self.rest = rest
        self.bad = bad

